﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;
using Entidades;

namespace Manager.App_code
{
    public class ServerController
    {
        private Thread[] serverThreads = new Thread[5];
        private Object mutex = null;

        public void IniciarServicio(Object mutex)
        {
            this.mutex = mutex;
            Thread th = new Thread(InterrogarServidores);
            th.Start();
        }


        private void InterrogarServidores()
        {
            try
            {                
                while (true)
                {
                    Thread.Sleep(1000); // Duerme un segundo

                    int count = 0;
                    foreach (ServerNode s in Data.lstServidores)
                    {
                        serverThreads[count] = new Thread(EnviarComandosAgente);
                        serverThreads[count].Start(s);
                        count++;
                    }
                    if (serverThreads[0] != null) serverThreads[0].Join();
                    if (serverThreads[1] != null) serverThreads[1].Join();
                    if (serverThreads[2] != null) serverThreads[2].Join();
                    if (serverThreads[3] != null) serverThreads[3].Join();
                    if (serverThreads[4] != null) serverThreads[4].Join();
                }
            }
            catch (Exception ex)
            {
                Logguer.log.Error("InterrogarServidores", ex);
            }
        }

        private void EnviarComandosAgente(object objServidor)
        {
            ServerNode srv = null;
            try
            {
                Thread[] ths = new Thread[3];
                srv = (ServerNode)objServidor;

                ths[0] = new Thread(EnviarRequestAgenteRam);
                ths[0].Start(srv);
                ths[1] = new Thread(EnviarRequestAgenteDisco);
                ths[1].Start(srv);
                ths[2] = new Thread(EnviarRequestAgenteCpu);
                ths[2].Start(srv);

                if (ths[0] != null) ths[0].Join();
                if (ths[1] != null) ths[1].Join();
                if (ths[2] != null) ths[2].Join();
            }
            catch (Exception)
            {                
                Logguer.log.Info("El servidor " + srv.Nombre + " (" + srv.URL + ") no responde");                
                srv.Activo = false;
            }
        }

        private void EnviarRequestAgenteRam(object objServidor)
        {
            ServerNode srv = null;
            lock (this.mutex)
            {
                try
                {
                    AgenteService.AgenteService servicio = new Manager.AgenteService.AgenteService();
                    srv = (ServerNode)objServidor;
                    servicio.Url = srv.URL;
                    Manager.AgenteService.MemoryResponse respuesta = servicio.ObtenerMemoria();
                }
                catch(Exception)
                {
                    Logguer.log.Info("El servidor " + srv.Nombre + " (" + srv.URL + ") no responde");
                    srv.Activo = false;
                }
            }
        }
        private void EnviarRequestAgenteDisco(object objServidor)
        {
            ServerNode srv = null;
            lock (this.mutex)
            {
                try
                {
                    AgenteService.AgenteService servicio = new Manager.AgenteService.AgenteService();
                    srv = (ServerNode)objServidor;
                    servicio.Url = srv.URL;
                    Manager.AgenteService.DiscoResponse respuesta = servicio.ObtenerDisco();
                }
                catch (Exception)
                {
                    Logguer.log.Info("El servidor " + srv.Nombre + " (" + srv.URL + ") no responde");
                    srv.Activo = false;
                }
            }
        }
        private void EnviarRequestAgenteCpu(object objServidor)
        {
            ServerNode srv = null;
            lock (this.mutex)
            {
                try{
                AgenteService.AgenteService servicio = new Manager.AgenteService.AgenteService();
                srv = (ServerNode)objServidor;
                servicio.Url = srv.URL;
                Manager.AgenteService.CpuResponse respuesta = servicio.ObtenerCpu();
                }
                catch (Exception)
                {
                    Logguer.log.Info("El servidor " + srv.Nombre + " (" + srv.URL + ") no responde");
                    srv.Activo = false;
                }
            }
        }

    }
}
